import Mock from "mockjs";
import { MockMethod } from "vite-plugin-mock";
import utils from "../../../src/utils/utils";
import contextPath from "../../context-path";
const Random = Mock.Random;

Mock.Random.extend({
  phone: function () {
    var phonePrefixs = ["138", "135", "189"]; // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/); //Number()
  },
});

// 确保每次请求的组织机构树一样，刷新页面后更新
const cache = [];

export default [
  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/organization.listing",
    method: "post",
    response: (options) => {
      const rows: any[] = [];
      const total = 112;
      let cnt = options.body.rows;
      if (options.body.rows * options.body.page > total) {
        cnt = total - options.body.rows * (options.body.page - 1);
      }
      for (let i = 0; i < cnt; i++) {
        const template = {
          orgId: Random.guid(),
          parentId: Random.guid(),
          orgName: Random.region(),
          orgCode: Random.string("upper", 5),
          levelCode: Random.natural(60, 1000),
          deptName: Random.city(),
          deptCode: Random.string("upper", 5),
          category: Mock.mock({
            "array|1": ["UNIT", "DEPT"],
          }).array,
          societyCode: Random.natural(),
          telephone: Random.phone(),
          description: Random.cparagraph(2),
          status: Mock.mock({
            "array|1": [0, 1],
          }).array,
        };
        rows.push(template);
      }

      return {
        code: 200,
        total: total,
        rows: rows,
      };
    },
  },
  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH + "/admin/api/organization.get",
    method: "post",
    response: (options) => {
      let orgId = options.body.orgId;
      const template = {
        orgId: orgId,
        parentId: Random.guid(),
        orgFullName: Random.region(),
        orgName: Random.region(),
        orgCode: Random.string("upper", 5),

        levelCode: Random.natural(60, 1000),
        deptName: Random.city(),
        deptCode: Random.string("upper", 5),
        category: Mock.mock({
          "array|1": ["UNIT", "DEPT"],
        }).array,
        societyCode: Random.natural(),
        telephone: Random.phone(),
        description: Random.cparagraph(2),
        status: Mock.mock({
          "array|1": [0, 1],
        }).array,
        children: [],
      };

      return {
        code: 200,
        message: "",
        data: template,
      };
    },
  },
  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/organization.extension.get",
    method: "post",
    response: (options) => {
      let orgId = options.body.orgId;
      const template = {
        orgId: orgId,
        parentId: Random.guid(),
        orgFullName: Random.region(),
        orgName: Random.region(),
        orgCode: Random.string("upper", 5),
        orgPath: "root-root,shanghai",
        levelCode: Random.natural(60, 1000),
        deptName: Random.city(),
        deptCode: Random.string("upper", 5),
        category: Mock.mock({
          "array|1": ["UNIT", "DEPT"],
        }).array,
        societyCode: Random.natural(),
        telephone: Random.phone(),
        description: Random.cparagraph(2),
        status: Mock.mock({
          "array|1": [0, 1],
        }).array,
        children: [],
      };

      return {
        code: 200,
        message: "",
        data: template,
      };
    },
  },

  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/organization.update",
    method: "post",
    response: (options) => {
      return {
        code: 200,
        message: "",
        data: null,
      };
    },
  },
  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/organization.delete",
    method: "post",
    response: (options) => {
      return {
        code: 200,
        message: "",
        data: null,
      };
    },
  },
  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/organization.status.change",
    method: "post",
    response: (options) => {
      return {
        code: 200,
        message: "",
        data: null,
      };
    },
  },
  {
    url:
      contextPath.VITE_API_ADMIN_CONTEXT_PATH +
      "/admin/api/organization.children.listing",
    method: "post",
    response: (options) => {
      const nodes: any[] = [];

      let parentId = options.body.parentId;
      // 过滤 UNIT、DEPT、默认空值显示全部
      let filter = options.body.categoryFilter;
      // console.log("categoryFilter", filter);
      if (parentId == "0") {
        let node = {
          orgId: "root-root",
          parentId: parentId,
          orgName: "总公司",
          orgCode: "ROOT",
          category: "UNIT",
          leaf: false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "root-root") {
        let node = {
          orgId: "caiwubu",
          parentId: "root-root",
          orgName: "财务部",
          orgCode: "caiwubu",
          leaf: false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "root-root") {
        let node = {
          orgId: "resshibu",
          parentId: "root-root",
          orgName: "人事部",
          orgCode: "resshibu",
          leaf: false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "root-root") {
        let node = {
          orgId: "xingzhegbu",
          parentId: "root-root",
          orgName: "行政部",
          orgCode: "xingzhegbu",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "root-root") {
        let node = {
          orgId: "xuanchuanbu",
          parentId: "root-root",
          orgName: "宣传部",
          orgCode: "xuanchuanbu",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "root-root") {
        let node = {
          orgId: "shenjibu",
          parentId: "root-root",
          orgName: "审计部",
          orgCode: "shenjibu",
          leaf: false,
        };
        nodes.push(node);
      }
      if (parentId == "caiwubu") {
        let node = {
          orgId: "caiwu1zu",
          parentId: "caiwubu",
          orgName: "财务一组",
          orgCode: "caiwu1zu",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "caiwubu") {
        let node = {
          orgId: "caiwu2zu",
          parentId: "caiwubu",
          orgName: "财务二组",
          orgCode: "caiwu2zu",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "resshibu") {
        let node = {
          orgId: "resshibu1",
          parentId: "resshibu",
          orgName: "公积金组",
          orgCode: "resshibu1",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "resshibu") {
        let node = {
          orgId: "resshibu2",
          parentId: "resshibu",
          orgName: "福利组",
          orgCode: "resshibu2",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "resshibu") {
        let node = {
          orgId: "resshibu3",
          parentId: "resshibu",
          orgName: "工资组",
          orgCode: "resshibu3",
          leaf: true,
        };
        nodes.push(node);
      }

      if (parentId == "root-root") {
        let node = {
          orgId: "shanghai",
          parentId: "root-root",
          orgName: "上海分公司",
          orgCode: "shanghai",
          category: "UNIT",
          leaf: filter == "UNIT" ? true : false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "shanghai") {
        let node = {
          orgId: "shanghai1",
          parentId: "shanghai",
          orgName: "外贸部",
          orgCode: "shanghai1",
          leaf: false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "shanghai") {
        let node = {
          orgId: "shanghai2",
          parentId: "shanghai",
          orgName: "收运部",
          orgCode: "shanghai2",
          leaf: true,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "shanghai1") {
        let node = {
          orgId: "shanghai11",
          parentId: "shanghai1",
          orgName: "外贸部1组",
          orgCode: "shanghai11",
          leaf: true,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "shanghai1") {
        let node = {
          orgId: "shanghai12",
          parentId: "shanghai1",
          orgName: "外贸部2组",
          orgCode: "shanghai12",
          leaf: true,
          children: [],
        };
        nodes.push(node);
      }

      if (parentId == "0") {
        let node = {
          orgId: "guangzhou",
          parentId: "0",
          orgName: "广州投资公司",
          orgCode: "guangzhou",
          category: "UNIT",
          leaf: filter == "UNIT" ? true : false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "guangzhou") {
        let node = {
          orgId: "guangzhou1",
          parentId: "guangzhou",
          orgName: "期货部",
          orgCode: "guangzhou1",
          leaf: false,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "guangzhou") {
        let node = {
          orgId: "guangzhou2",
          parentId: "guangzhou",
          orgName: "基金部",
          orgCode: "guangzhou2",
          leaf: true,
          children: [],
        };
        nodes.push(node);
      }
      if (parentId == "guangzhou1") {
        let node = {
          orgId: "guangzhou11",
          parentId: "guangzhou1",
          orgName: "期货部一组",
          orgCode: "guangzhou11",
          leaf: true,
        };
        nodes.push(node);
      }
      if (parentId == "guangzhou1") {
        let node = {
          orgId: "guangzhou12",
          parentId: "guangzhou1",
          orgName: "期货部二组",
          orgCode: "guangzhou12",
          leaf: true,
        };
        nodes.push(node);
      }

      if (filter != "") {
        for (let i = nodes.length - 1; i >= 0; i--) {
          if (filter == "UNIT") {
            if (nodes[i].category != "UNIT") {
              nodes.splice(i, 1);
            }
          } else if (filter == "DEPT") {
            if (nodes[i].category == "UNIT") {
              nodes.splice(i, 1);
            }
          }
        }
      }
      // if (parentId == "0") {
      //   // 根节点

      //   if (cache.length == 0) {
      //     let node = {
      //       orgId: Random.guid(),
      //       parentId: parentId,
      //       orgName: Random.region(),
      //       orgCode: "ROOT",
      //       leaf: false,
      //     };
      //     nodes.push(node);
      //     cache.push(node);
      //   } else {
      //     // 从缓存中读取
      //     cache.forEach((m) => {
      //       if (m.parentId == parentId) {
      //         nodes.push(m);
      //       }
      //     });
      //   }
      // } else {
      //   // 子节点
      //   cache.forEach((m) => {
      //     if (m.id == parentId) {
      //       nodes.push(m);
      //     }
      //   });

      //   if (nodes.length == 0) {
      //     // 缓存中没有则新建
      //     const cnt = Mock.mock({
      //       "number|1-5": 5,
      //     });
      //     for (let i = 0; i < cnt.number; i++) {
      //       const template = {
      //         orgId: Random.guid(),
      //         parentId: parentId,
      //         orgName: Random.ctitle(),
      //         orgCode: Random.string("upper", 5),
      //         leaf: Mock.mock({
      //           "boolean|1": true,
      //         }).boolean,
      //       };
      //       nodes.push(template);
      //       cache.push(template);
      //     }
      //   }
      // }

      return {
        code: 200,
        message: "",
        data: nodes,
      };
    },
  },
] as MockMethod[];
